import { defineStore } from "pinia";
export const usetabsBarStore = defineStore("tabsBar", {
  state() {
    return {
      visitedRoutes: [],
    };
  },
  getters: {
    getVisitedRoutes:(state)=> {
      return state.visitedRoutes;
    },
    // name() {
    //   return this.userInfo.name || ''
    // },
    // avatar() {
    //   return this.userInfo.avatar || ''
    // },
    // role(){
    //   return this.userInfo.role || []
    // },
  },
  actions: {
    /**
     * @description 添加标签页
     * @param {*} route
     */
    addVisitedRoute(route) {
      let target = this.visitedRoutes.find((item) => item.path === route.path);
      if (target) {
        if (route.fullPath !== target.fullPath) Object.assign(target, route);
        return;
      }
      this.visitedRoutes.push(Object.assign({}, route));
    },
    /**
     * @description 删除当前标签页
     * @param {*} route
     */
    delVisitedRoute(route) {
        this.visitedRoutes.forEach((item, index) => {
            if (item.path === route.path) this.visitedRoutes.splice(index, 1)
          })
    },
    /**
     * @description 删除当前标签页以外其它全部多标签页
     * @param {*} route
     */
    delOthersVisitedRoutes(route) {
        this.visitedRoutes = this.visitedRoutes.filter(
            (item) => item.meta.affix || item.path === route.path
          )
    },
    /**
     * @description 删除当前标签页左边全部多标签页
     * @param {*} route
     */
    delLeftVisitedRoutes(route) {
        let index = this.visitedRoutes.length
        this.visitedRoutes = this.visitedRoutes.filter((item) => {
          if (item.name === route.name){
            index = this.visitedRoutes.indexOf(item)
          }
          return item.meta.affix || index <= this.visitedRoutes.indexOf(item)
        })
    },
    /**
     * @description 删除当前标签页右边全部多标签页
     * @param {*} route
     */
    delRightVisitedRoutes(route) {
        let index = this.visitedRoutes.length
        this.visitedRoutes = this.visitedRoutes.filter((item) => {
          if (item.name === route.name){
            index = this.visitedRoutes.indexOf(item)
          }
          return item.meta.affix || index >= this.visitedRoutes.indexOf(item)
        })
    },
    /**
     * @description 删除全部多标签页
     */
    delAllVisitedRoutes() {
        this.visitedRoutes = this.visitedRoutes.filter((item) => item.meta.affix)
    },
  },
});
